<template>
    <div>
        <el-page-header content="个人中心" icon="" title="企业门户管理系统"></el-page-header>
        <el-row :gutter="20" class="el-row">
            <el-col :span="6">
                <el-card shadow="always" class="box-card">
                    <el-avatar :size="100" shape="circle" :src="avatarUrl" fit="fill"></el-avatar>
                    <h3>{{ store.state.userInfo.username }}</h3>
                    <h5>{{ store.state.userInfo.role === 1 ? "管理员" : "编辑" }}</h5>
                </el-card>
            </el-col>
            <el-col :span="18">
                <el-card shadow="always">
                    <template #header>
                        <div>
                            <span>个人信息</span>
                        </div>
                    </template>
                    <!-- card body -->
                    <el-form ref="userFormRef" :model="userForm" :rules="userFormRules" label-width="120px"
                        class="demo-ruleForm" status-icon>
                        <el-form-item label="用户名" prop="username">
                            <el-input v-model="userForm.username" />
                        </el-form-item>
                        <el-form-item label="性别" prop="gender">
                            <el-select v-model="userForm.gender" placeholder="Select" style="width: 100%;">
                                <el-option v-for="item in options" :key="item.value" :label="item.label"
                                    :value="item.value" />
                            </el-select>
                        </el-form-item>
                        <el-form-item label="个人简介" prop="introduction">
                            <el-input type="textarea" v-model="userForm.introduction" />
                        </el-form-item>
                        <el-form-item label="头像" prop="avatar">
                            <Upload :avatar="userForm.avatar" @uploadChange="handleChange"></Upload>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="submitForm">提交</el-button>
                        </el-form-item>
                    </el-form>
                </el-card>

            </el-col>
        </el-row>

    </div>
</template>


<script setup>
import { useStore } from 'vuex';
import { computed, ref, reactive } from 'vue';
import { ElMessage } from 'element-plus'
import upload from '@/util/upload'
import Upload from '@/components/upload/Upload'
const store = useStore()
// console.log(store.state.userInfo);
// 头像地址
const avatarUrl = computed(() => store.state.userInfo.avatar ? 'http://localhost:3000' + store.state.userInfo.avatar : `https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png`)

// 结构赋值
const { username, gender, introduction, avatar } = store.state.userInfo
// 关联userForm表单ref
const userFormRef = ref()
// 双向绑定userForm表单数据
const userForm = reactive({
    username,
    gender,
    introduction,
    avatar,
    file: null
})
// 定义规则校验
const userFormRules = reactive({
    username: [
        { required: true, message: '请输入你的名字', trigger: 'blur' },
    ],
    gender: [
        { required: true, message: '请选择你的性别', trigger: 'blur' },
    ],
    introduction: [
        { required: true, message: '请输入个人简介', trigger: 'blur' },
    ],
    avatar: [
        { required: true, message: '请上传头像', trigger: 'blur' },
    ],

})
// 性别选择字段
const options = [
    {
        label: "保密",
        value: 0
    },
    {
        label: "男",
        value: 1
    },
    {
        label: "女",
        value: 2
    },
]
// 选择完图片的回调
const handleChange = (file) => {
    userForm.avatar = URL.createObjectURL(file)
    userForm.file = file
}

// 更新提交
const submitForm = () => {
    userFormRef.value.validate(async (valid) => {
        if (valid) {
            // console.log("submit", userForm);
            // 提交包含文件等复杂数据的时候需要使用到formdata
            const res = await upload("/adminapi/user/upload", userForm)
            // console.log(res.data);
            if (res.ActionType === 'OK') {
                store.commit("changeUserInfo", res.data)
                ElMessage.success("更新成功")
            }
        } else {
            ElMessage.error("更新失败")
        }
    })
}
</script>




<style lang="scss" scoped>
.el-row {
    margin-top: 20px;

    .box-card {
        text-align: center;
    }
}
</style>